import {
	CustomViewActionType,
	ICustomViewAbility,
	ICustomViewController,
	ICustomViewStore,
	IDECtrlAbility,
	IEditFormAbility,
	IParam,
	ISearchFormAbility,
	IViewActionResult,
	SearchFormActionType,
} from '@/core/interface';
import { deepCopy } from '@/core/utils';
import { DEViewController } from './de-view-controller';

/**
 * 自定义视图控制器
 *
 * @export
 * @class EditViewController
 * @extends {DEViewController<T, S, A>}
 * @implements {IEditViewController}
 */
export class CustomViewController
	extends DEViewController<
		CustomViewActionType,
		ICustomViewStore,
		ICustomViewAbility
	>
	implements ICustomViewController<ICustomViewStore, ICustomViewAbility>
{
	/**
	 * 视图加载
	 *
	 * @param {IParam} [opts={}]
	 * @memberof CustomViewController
	 */
	public viewMounted(opts: IParam = {}) {
		super.viewMounted(opts);
		if (this.model.useDefaultLayout) {
			if (!this.isLoadDefault) {
				this.isLoadDefault = true;
				return;
			}
			this.load(opts);
		} else {
			this.initLayout().then(() => {
				this.load(opts);
			});
		}
	}

	/**
	 * 视图加载
	 *
	 * @param {IParam} [args]
	 * @return {*}  {Promise<IViewActionResult>}
	 * @memberof CustomViewController
	 */
	public load(args?: IParam): Promise<IViewActionResult> {
		const searchForm = this.getSubAbility<ISearchFormAbility>('searchForm');
		if (searchForm) {
			const tempViewParams = deepCopy(this.store.viewParams);
			return searchForm.loadDraft(tempViewParams);
		}
		return this.ctrlLoad(args);
	}

	/**
	 * 刷新视图
	 *
	 * @param {(IParam | undefined)} [args]
	 * @return {*}  {Promise<boolean>}
	 * @memberof CustomViewController
	 */
	 public async refresh(args?: IParam | undefined): Promise<IViewActionResult> {
		return (await this.load(args))
	}

	/**
	 * 部件加载
	 *
	 * @memberof CustomViewController
	 */
	ctrlLoad(opts: IParam = {}): Promise<IViewActionResult> {
		const { ctrlNameArray } = this.model;
		return ctrlNameArray.forEach((name: string) => {
			if (name === 'form') {
				let action: 'loadDraft' | 'load' = 'loadDraft';
				if (
					this.model.entityCodeName &&
					this.store.context[this.model.entityCodeName.toLowerCase()]
				) {
					action = 'load';
				}
				const ctrl = this.getSubAbility<IEditFormAbility>(name);
				const tempViewParams = deepCopy(this.store.viewParams);
				ctrl[action](Object.assign(tempViewParams, opts));
			} else if (name !== 'searchForm') {
				const ctrl = this.getSubAbility<IDECtrlAbility>(name);
				if (ctrl) {
					const tempViewParams = deepCopy(this.store.viewParams);
					ctrl.load(Object.assign(tempViewParams, opts));
				}
			}
		});
	}

	/**
	 * 处理搜索表单行为
	 *
	 * @protected
	 * @param {SearchFormActionType} action
	 * @param {IParam[]} data
	 * @memberof CustomViewController
	 */
	protected handleSearchFormAction(
		action: SearchFormActionType,
		data: IParam[]
	) {
		if (action === 'load' || action === 'search') {
			this.ctrlLoad();
		}
	}

	/**
	 * 处理部件行为
	 *
	 * @param {string} name
	 * @param {string} action
	 * @param {IParam[]} data
	 * @return {*}  {void}
	 * @memberof CustomViewController
	 */
	public handleCtrlAction(name: string, action: string, data: IParam[]): void {
		super.handleCtrlAction(name, action, data);
		if (name === 'searchForm') {
			this.handleSearchFormAction(action as SearchFormActionType, data);
			return;
		}
	}
}
